﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChuongtrinhQLHSTHPT
{
    public partial class lapthoikhoabieu : Form
    {
        public lapthoikhoabieu()
        {
            InitializeComponent();
        }

        private void lapthoikhoabieu_Load(object sender, EventArgs e)
        {
            Connectionclass con = new Connectionclass();
            datatkb.DataSource = con.Loadtkb();
        }

        public int MATKB;
        private void datatkb_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                MATKB = Int32.Parse(datatkb.Rows[e.RowIndex].Cells[0].Value.ToString());
                int LOP = Int32.Parse(datatkb.Rows[e.RowIndex].Cells[1].Value.ToString());
                string TENLOP = datatkb.Rows[e.RowIndex].Cells[2].Value.ToString();
                int MONHOC = Int32.Parse(datatkb.Rows[e.RowIndex].Cells[3].Value.ToString());
                string TENMON = datatkb.Rows[e.RowIndex].Cells[4].Value.ToString();
                int GVGIANGDAY = Int32.Parse(datatkb.Rows[e.RowIndex].Cells[5].Value.ToString());
                string HOTEN = datatkb.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtmatkb.Text = MATKB.ToString();
                txttkb_lop.Text = LOP.ToString();
                txttkb_mon.Text = MONHOC.ToString();
                txttkb_gv.Text = GVGIANGDAY.ToString();
            }
            catch
            {
                MessageBox.Show("Bạn chưa chọn đúng bản ghi !");
            }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            MATKB = Int32.Parse(txtmatkb.Text);
            int LOP = Int32.Parse(txttkb_lop.Text);
            int MONHOC = Int32.Parse(txttkb_mon.Text);
            int GVGIANGDAY = Int32.Parse(txttkb_gv.Text);
            if ((Convert.ToString(MATKB) != "") && (Convert.ToString(LOP) != "") && (Convert.ToString(MONHOC) != "") && (Convert.ToString(GVGIANGDAY) != ""))
            {
                Connectionclass connection = new Connectionclass();
                bool Inserted = connection.InsertTKB(MATKB, LOP, MONHOC, GVGIANGDAY);
                if (Inserted == true)
                {
                    MessageBox.Show("Thêm thành công!");
                    datatkb.DataSource = new Connectionclass().Loadtkb();
                    txtmatkb.Text = "";
                    txttkb_lop.Text = "";
                    txttkb_mon.Text = "";
                    txttkb_gv.Text = "";
                }
                else
                {
                    MessageBox.Show("Không thể thêm dữ liệu !");
                }
            }
            else
            {
                MessageBox.Show("Chưa điền đầy đủ thông tin !");
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            MATKB = Int32.Parse(txtmatkb.Text);
            int LOP = Int32.Parse(txttkb_lop.Text);
            int MONHOC = Int32.Parse(txttkb_mon.Text);
            int GVGIANGDAY = Int32.Parse(txttkb_gv.Text);
            bool isUpdate = new Connectionclass().Updatetkb(MATKB, LOP, MONHOC, GVGIANGDAY);
            if (isUpdate == true)
            {
                datatkb.DataSource = new Connectionclass().Loadtkb();
                MessageBox.Show("Cập nhật thành công !");
                txtmatkb.Text = "";
                txttkb_lop.Text = "";
                txttkb_mon.Text = "";
                txttkb_gv.Text = "";
            }
            else
            {
                MessageBox.Show("Cập nhật không thành công!");
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (MATKB > 0)
            {
                DialogResult result;
                result = MessageBox.Show("Ban co chac chan muon xoa hay khong ?", "Cua so nhac nho", MessageBoxButtons.OKCancel);

                if (result == System.Windows.Forms.DialogResult.OK)
                {

                    bool isDelete;
                    isDelete = new Connectionclass().Deletetkb(MATKB);
                    if (isDelete)
                    {
                        MessageBox.Show("Xóa thành công!");
                        MATKB = -1;
                        datatkb.DataSource = new Connectionclass().Loadtkb();
                        txtmatkb.Text = "";
                        txttkb_lop.Text = "";
                        txttkb_mon.Text = "";
                        txttkb_gv.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Xóa không thành công!");
                    }
                }
            }
            else
            {
                MessageBox.Show("Chưa có bản ghi nào được chọn!");
            }
        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            datatkb.Visible = true;
            if (txtshearch.Text != "")
            {
                datatkb.DataSource = new Connectionclass().Searchtkb(Int32.Parse(txtshearch.Text));
                if (datatkb.RowCount <= 1)
                {
                    datatkb.Visible = false;
                    MessageBox.Show("Thông tin chưa chính xác");
                }
            }
            else
            {
                datatkb.DataSource = new Connectionclass().Loadtkb();
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
